package com.freeter.modules.gjs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.freeter.modules.gjs.entity.UsersSites;
import com.freeter.modules.gjs.mapper.UsersSitesMapper;
import com.freeter.modules.gjs.service.UsersSitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UsersSitesServiceImpl implements UsersSitesService {
    @Autowired
    private UsersSitesMapper usersSitesMapper;

    @Override
    public List<UsersSites> getAllUsersSites() {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        return usersSitesMapper.selectList(wrapper);
    }

    @Override
    public UsersSites getUsersSitesById(Long id) {
        return usersSitesMapper.selectById(id);
    }

    @Override
    public List<UsersSites> getUsersSitesByUserId(Long userId) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("is_deleted", 0);
        return usersSitesMapper.selectList(wrapper);
    }

    @Override
    public List<UsersSites> getUsersSitesBySiteCode(String siteCode) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("site_code", siteCode).eq("is_deleted", 0);
        return usersSitesMapper.selectList(wrapper);
    }

    @Override
    public UsersSites getUsersSitesByUserAndSite(Long userId, String siteCode) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("site_code", siteCode).eq("is_deleted", 0);
        List<UsersSites> list = usersSitesMapper.selectList(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public UsersSites createUsersSites(UsersSites usersSites) {
        usersSitesMapper.insert(usersSites);
        return usersSites;
    }

    @Override
    public UsersSites updateUsersSites(Long id, UsersSites usersSites) {
        usersSites.setId(id);
        usersSitesMapper.updateById(usersSites);
        return usersSites;
    }

    @Override
    public void deleteUsersSites(Long id) {
        usersSitesMapper.deleteById(id);
    }

    @Override
    public void deleteUsersSitesByUserAndSite(Long userId, String siteCode) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("site_code", siteCode);
        usersSitesMapper.delete(wrapper);
    }

    @Override
    public List<UsersSites> getUsersSitesByStatus(String status) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        wrapper.eq("status", status).eq("is_deleted", 0);
        return usersSitesMapper.selectList(wrapper);
    }

    @Override
    public List<UsersSites> getUsersSitesByCondition(String name, Integer state) {
        Wrapper<UsersSites> wrapper = new EntityWrapper<>();
        if (name != null) wrapper.eq("name", name);
        if (state != null) wrapper.eq("state", state);
        wrapper.eq("is_deleted", 0);
        return usersSitesMapper.selectList(wrapper);
    }
} 